package com.xxm.thoughtworksdemo

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions


class ImagesAdapter constructor(var context: Context, var images: ArrayList<ImageBean>) : BaseAdapter() {
    private val imageSize = (getScreenWidth(context) - 2 * dp2px(context,4f) - dp2px(context,84f)) / 3

    override fun getCount(): Int {
        return images.size
    }

    override fun getItem(position: Int): ImageBean {
        return images[position]
    }

    override fun getItemId(position: Int): Long {
        return getItem(position).hashCode().toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val imageView = ImageView(context)
        imageView.setBackgroundColor(ContextCompat.getColor(context, com.xxm.thoughtworksdemo.R.color.colorF2))
        imageView.layoutParams =
            ViewGroup.LayoutParams(imageSize, imageSize)
        val url: String? = getItem(position).url
        Glide.with(context).load(url)
            .override(imageSize,imageSize)
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(imageView)
        return imageView
    }
}